package com.example.test09api.api;

import com.alibaba.fastjson.JSONObject;
import com.example.test09api.common.util.HttpClientUtils;

/**
 * @Author: fxy
 * @Date: 2022/01/10/14:00
 * @Description:
 */
public class Weather {
    public static void main(String[] args) {
        String result  = getLocation("114.399869","30.505838");
        System.out.println(result);
    }

    private static String getLocation(String lon, String lat){
        String location = lat+","+lon;
        String baiduAK = "百度地图开放平台中用户申请注册的key";
        String url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak="+baiduAK+"&output=json&coordtype=wgs84ll&location="+location;
        try {
            String s = HttpClientUtils.doGet(url);
            JSONObject object = JSONObject.parseObject(s);
            String status = object.get("status").toString();
            if ("0".equals(status)){
                JSONObject result = (JSONObject) object.get("result");
                JSONObject addressComponent = (JSONObject) result.get("addressComponent");
                return (String) addressComponent.get("city");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
